﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Farigola.Engine.SystemData.Entity;
using Farigola.Engine.SystemData.Entity.Type;
using Farigola.Engine.SystemData.Entity.inst;

namespace Farigola.Engine.TestUnit
{
    /// <summary>
    /// Test the entity folder, the type and instances.
    /// </summary>
    [TestFixture]
    public class EntityFolderTestUnit
    {
        //---------------------------------------------------------------------
        /// <summary>
        /// Check that the type Folder is present in the internal manager of type.
        /// </summary>
        [Test]
	    public void CheckTypeFolder() 
        {
            Engine engine = EngineCommonTestUnit.CreateInitEngine();
            Workspace wksp = engine.GetWorkspace();

		    // get/find the type folder from the system data mgr from the workspace by the name 
		    EntTypeFolder typeFolder = (EntTypeFolder) wksp.GetSystemDataMgr().FindEntTypeByName(EntityDef.TypeFolderName);
		    Assert.IsNotNull(typeFolder, "The type named 'folder' should exists.");

		    // get/find the type folder from the system data mgr from the workspace by the id 
		    EntityId id= new EntityId();
		    id.SetFromIdStr(EntityDef.TypeFolderValGuid);
		    typeFolder = (EntTypeFolder) wksp.GetSystemDataMgr().FindEntTypeById(id);
		    Assert.IsNotNull(typeFolder, "The type folder with the dedicated id should exists.");
	    }

        //---------------------------------------------------------------------
        [Test]
	    public void CreFolderUnderRootByNull() 
        {
            // create an engine, with a workspace
            Workspace wksp = EngineCommonTestUnit.CreateInitEngineWorkspace();
		
		    // create a project
		    Project prj= wksp.CreateActiveProject("prj");

		    // get the root folder user, contains all entities adds by the user
		    EntInstFolder rootFolderUser= prj.GetRootUserFolder();

		    // create 2 folders, add it under the root folder 
		    EntInstFolder fold = prj.createEntInstFolder(null, "fold");
		    EntInstFolder fold2 = prj.createEntInstFolder(rootFolderUser, "fold2");
				
		    // check the folders
		    Assert.IsNotNull(fold, "The folder 'fold' should exists.");
		    Assert.AreEqual("fold",fold.GetName(),"xxx");
		    Assert.AreEqual(EntityDef.TypeSystOrUser.User, fold.GetTypeSystOrUser(), "The folder 'fold' should be a 'user' type.");
		    Assert.IsNotNull(fold2, "The folder 'fold2' should exists.");
		    Assert.AreEqual("fold2",fold2.GetName(), "xxx");
		    Assert.AreEqual(EntityDef.TypeSystOrUser.User, fold2.GetTypeSystOrUser(),"The folder 'fold2' should be a 'user' type.");
		
		    // the created folder has no child
            Assert.AreEqual(0, fold.ChildCount(), "The folder 'fold' has no child.");
            Assert.AreEqual(0, fold2.ChildCount(), "The folder 'fold2' has no child.");
		
		    // check that the rootfolderUser has these 2 folders as childs	
		    // todo:??
	    }


        //---------------------------------------------------------------------
        [Test]
        public void CreUnderRootFolderWrongName()
        {
            // create an engine, with a workspace
            Workspace wksp = EngineCommonTestUnit.CreateInitEngineWorkspace();
		
		    // create a project
		    Project prj= wksp.CreateActiveProject("prj");

		    // get the root folder user, contains all entities adds by the user
		    //EntInstFolder rootFolderUser= prj.getRootUserFolder();

		    // try to create a folder but wrong  
		    EntInstFolder fold = prj.createEntInstFolder(null, null);
		    Assert.IsNull(fold, "The folder can't created because the name is null.");
		
		    // try to create a folder but wrong  
		    fold = prj.createEntInstFolder(null, "");
		    Assert.IsNull(fold,"The folder can't created because the name is empty.");
		
		    // try to create a folder but wrong  
		    fold = prj.createEntInstFolder(null, " ");
		    Assert.IsNull(fold,"The folder can't created because the name is blank.");
	    }

        //---------------------------------------------------------------------
        /// <summary>
        /// save and load a prj with a folder in folder.
        /// </summary>
        [Test]
        public void CreFolderInFolder()
        {
            // create an engine, with a workspace
            Workspace wksp = EngineCommonTestUnit.CreateInitEngineWorkspace();

            // create a project
            Project prj = wksp.CreateActiveProject("prj");
            EntInstFolder folder = prj.createEntInstFolder("fold");
            EntInstFolder folderChild = prj.createEntInstFolder(folder, "foldChild");


            // checks fold\foldChild
            EntInstFolder folderFind = prj.GetEntityFinder().FindEntInstFolderByName(prj.GetRootUserFolder(), "fold", false);
            Assert.IsNotNull(folderFind, "The folder 'fold' under the root user folder should exists.");

            // checks fold\foldChild
            EntInstFolder folderChildFind = prj.GetEntityFinder().FindEntInstFolderByName(folderFind, "foldChild", false);
            Assert.IsNotNull(folderFind, "The folder 'foldChild' under the folder 'fold' should exists.");
        }

	
	// creer hiérarchie de plusieurs folders
	// fold -> fold -> fold
	

	
	// tester find de child d'un folder (que child direct)
	//EntityBase findChildByName(String name)
	
	//EntityBase findChildById(EntityId id)

	
	// find this folder: que les childs direct 1seul niv, ou sinon en profondeur
	// que dans les user? oui dans la methode générale
	// pour un parcours avec les sytem, faire autre méthode
	
	// du coup, ecrire le EntityFinder
	//prj.findChildByName(String name)
    }
}
